const BASE_URL = '/api';

const json2url = (json) => {
    let arr = [];
    for (let name in json) {
        arr.push(name + '=' + json[name]);
    }
    return arr.join('&')
}
const http = ({ url, method = 'get', params = {}, data = {} }) => {
    if (!url) return;

    const token = sessionStorage.getItem('token') || '';
    let option = {
        method,
        headers: {
            token
        }
    }
    if(method==='post'){
        option.body=JSON.stringify(data);
    }
    return fetch(BASE_URL + url + '?' + json2url(params),option).then(response => response.json())
}

export default http;